package com.rj.core.constant;

import com.rj.core.check.SFunctionCompare;
import com.rj.util.CalcUtils;

/**
 * 数值比较类型
 *
 * @author YFX_RANLUBO
 * @date 2024/09/15 17:57
 **/
public enum CompareType {
    /**
     * 等于
     */
    EQ("等于", (v, d) -> compare(v, d) == 0),

    /**
     * 大于等于
     */
    GE("大于等于", (v, d) -> compare(v, d) >= 0),
    /**
     * 大于
     */
    GT("大于", (v, d) -> compare(v, d) > 0),
    /**
     * 小于等于
     */
    LE("小于等于", (v, d) -> compare(v, d) <= 0),
    /**
     * 小于
     */
    LT("小于", (v, d) -> compare(v, d) < 0);
    /**
     * 比较执行器
     */
    private SFunctionCompare compare;
    /**
     * 描述
     */
    private String desc;

    CompareType(String desc, SFunctionCompare compare) {
        this.compare = compare;
        this.desc = desc;
    }

    /**
     * 获得原始比较结果
     *
     * @param v
     * @param d
     * @return
     */
    private static <V> int compare(V v, V d) {
        if (d instanceof Number) {
            double comp = ((Number) v).doubleValue() - ((Number) d).doubleValue();
            return comp > 0 ? 1 : -1;
        }
        return CalcUtils.init(v).compareTo(d);
    }

    /**
     * 比较数值
     *
     * @return
     */
    public SFunctionCompare getCompare() {
        return compare;
    }

    /**
     * 操作描述
     *
     * @return
     */
    public String getDesc() {
        return desc;
    }
}
